package com.bw.module_home.ui

import android.content.Context
import android.util.Log
import android.widget.ImageView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseFragment
import com.bw.lib_common.base.BaseViewModel
import com.bw.lib_common.base.state.Default
import com.bw.lib_common.base.state.Empty
import com.bw.lib_common.base.state.Failed
import com.bw.lib_common.base.state.Loading
import com.bw.lib_common.base.state.Success
import com.bw.lib_common.observeWithLifecycle
import com.bw.lib_image.GlideApp
import com.bw.module_home.R
import com.bw.module_home.home.adapter.HomeMenuAdapter
import com.bw.module_home.home.adapter.HomeNoticeAdapter
import com.bw.module_home.databinding.FragmentHomeBinding
import com.bw.module_home.home.viewmodel.HomeViewModel
import com.youth.banner.loader.ImageLoader
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.HomeFragment)
class HomeFragment : BaseFragment<BaseViewModel, FragmentHomeBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_home
    private val mViewModel: HomeViewModel by viewModels()

    val mAdapter : HomeMenuAdapter by lazy {
        HomeMenuAdapter()
        HomeMenuAdapter().apply {
            onItemClick = { serviceModel ->
                val context = requireContext()
                when (serviceModel.title) {
                    "车位管理" -> ARouter.getInstance().build(Router.Ui.CarSpaceManagerActivity).navigation()
                    "访客登记" -> ARouter.getInstance().build(Router.Ui.VisitActivity).navigation()
                    "物业报修" -> ARouter.getInstance().build(Router.Ui.RepairManagerActivity).navigation()
                    "房源信息" -> ARouter.getInstance().build(Router.Ui.PropertyActivity).navigation()
                    "月卡会员" -> ARouter.getInstance().build(Router.Ui.MonthVipActivity).navigation()
                    "考勤打卡"  ->ARouter.getInstance().build(Router.Ui.AttendancesActivity).navigation()
                    "设备管理"  ->ARouter.getInstance().build(Router.Ui.DeviceActivity).navigation()
                    "商家推荐"  ->ARouter.getInstance().build(Router.Ui.MarketersActivity).navigation()
                }
            }
        }
    }

    val noticeAdapter : HomeNoticeAdapter by lazy {
        HomeNoticeAdapter()

    }
    override fun initView() {
        super.initView()
        mViewModel.initList()
        initAdapter()
        val list: MutableList<Int> = mutableListOf()
        list.add(R.drawable.home_top_image)
        list.add(R.drawable.banner1)
        list.add(R.drawable.banner2)

        mBinding.homeBanner.setImages(list).setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                GlideApp.with(context!!).load(path).transform(CenterCrop(),
                    RoundedCorners(20)
                ).into(imageView!!)
            }

        }).start()
    }

    //获取数据
    override fun initData() {
        mViewModel.getNotice(1,20)

    }
    //实现网络接口
    override fun initListener() {
//        mViewModel.mNotice
        mViewModel.noticeState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    Log.i("MYJ", "initListener: "+it.message)
                }
                Loading -> {}
                Success -> {
                    noticeAdapter.setData(mViewModel.noticeList.filter { it.title!=""&&it.title!="曾山峰爱导管" })
                    Log.i("MYJ", "initListener: "+mViewModel.noticeList.size)
                }

                Default -> TODO()
            }
        }
    }

    private fun initAdapter(){
        Log.d("MYJ", "initAdapter: "+mViewModel.ServiceList.size)
        mAdapter.setData(mViewModel.ServiceList)
        mBinding.homeMenuRv.layoutManager= GridLayoutManager(context,4)
        mBinding.homeMenuRv.adapter=mAdapter
        mBinding.homeRv.layoutManager= LinearLayoutManager(context)
        mBinding.homeRv.adapter=noticeAdapter
    }

}